package Dao;

import Bean.Aircraft;
import Bean.Ticket;
import Utils.DBUtil;
import com.sun.corba.se.pept.broker.Broker;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-02-28
 * Time: 17:09
 */
public class AircraftDao {

    private Connection connection = null;
    private PreparedStatement statement = null;
    private ResultSet resultSet = null;

    //1,新增飞机
    public boolean insert(Aircraft aircraft) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "insert into aircraft values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,aircraft.getAircraftId());
            statement.setString(2,aircraft.getModel());
            statement.setString(3,aircraft.getCurrentStatus().name());
            statement.setInt(4,aircraft.getMaxSeats());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    //2.1查询所有飞机
    public List<Aircraft> select (int pageNo, int pageSize) {
        List<Aircraft> array = null;
        try{
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from aircraft limit ?,?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,(pageNo-1)*pageSize);
            statement.setInt(2,pageSize);
            resultSet = statement.executeQuery();
            if(resultSet != null) {
                array = new ArrayList<>();
                while(resultSet.next()){
                    Aircraft aircraft = new Aircraft();
                    aircraft.setAircraftId(resultSet.getString("aircraft_id"));
                    aircraft.setModel(resultSet.getString("model"));
                    aircraft.setCurrentStatus(Aircraft.AircraftStatus.valueOf(resultSet.getString("current_status")));
                    aircraft.setMaxSeats(resultSet.getInt("max_seats"));
                    array.add(aircraft);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return array;
    }

    //2.2 查询指定飞机
    public Aircraft selectById (Aircraft aircraft) {
        Aircraft aircraft1 = null;
        try{
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from aircraft where aircraft_id = ?";
            System.out.println("[AircraftDao] 查询飞机是传入的参数为 : " + aircraft.getAircraftId());
            statement=connection.prepareStatement(sql);
            statement.setString(1,aircraft.getAircraftId());
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                aircraft1 = new Aircraft();
                aircraft1.setAircraftId(resultSet.getString("aircraft_id"));
                aircraft1.setModel(resultSet.getString("model"));
                aircraft.setCurrentStatus(Aircraft.AircraftStatus.valueOf(resultSet.getString("current_status")));
                aircraft1.setMaxSeats(resultSet.getInt("max_seats"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return aircraft1;
    }

    //3.删除飞机
    public boolean delete(Aircraft aircraft) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.拼装sql语句
            String sql = "delete from aircraft where aircraft_id = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,aircraft.getAircraftId());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    //4.修改飞机数据
    public boolean update(Aircraft aircraft) {
        try{
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.拼装sql语句
            connection = DBUtil.getConnection();
            String sql="UPDATE aircraft SET model = ?,current_status = ?,max_seats = ? where aircraft_id = ?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,aircraft.getModel());
            statement.setString(2,aircraft.getCurrentStatus().name());
            statement.setInt(3,aircraft.getMaxSeats());
            statement.setString(4,aircraft.getAircraftId());

            int ret = statement.executeUpdate();
            //3.执行sql
            if(ret == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    public int size() {
        int size = 0;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "SELECT COUNT(*) AS aircraft_count FROM aircraft";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            if(resultSet.next()) {
                size = resultSet.getInt("aircraft_count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return size;
    }
}
